package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDTO dto) {
        //校验参数
        dto.checkParam();
        if (dto==null){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 2. 封装条件 执行查询
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.<AdSensitive>lambdaQuery();

        // 敏感词模糊查询
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdSensitive::getSensitives, dto.getName());
        }
        // 序号升序
        queryWrapper.orderByAsc(AdSensitive::getId);
        // 分页
        Page<AdSensitive> pageReq = new Page<>(dto.getPage(), dto.getSize());
        // 执行查询
        IPage<AdSensitive> pageResult = this.page(pageReq, queryWrapper);
        // 3. 封装响应结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if (adSensitive==null){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        int count = count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count>0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"该敏感词已经存在");
        }
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive==null||adSensitive.getSensitives()==null){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        int count = count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count>0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"该敏感词已经存在");
        }
        updateById(adSensitive);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id==null){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdSensitive adSensitive = getById(id);
        if (adSensitive==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult();
    }

    @Autowired
    AdSensitiveMapper adSensitiveMapper;
    @Override
    public ResponseResult selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
